import { requestClient } from '#/api/request';

export namespace TransactionApi {
  /** 交易账单列表查询参数 */
  export interface TransactionListParams {
    page?: number;
    per_page?: number;
    transaction_id?: string;
    type?: 'income' | 'expense';
    sub_type?: string;
    status?: 'pending' | 'processing' | 'completed' | 'failed' | 'cancelled';
    landing_company?: string;
    start_date?: string;
    end_date?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 交易记录接口 */
  export interface TransactionRecord {
    id: string;
    transaction_id: string;
    transaction_number: string;
    tenant_id: number;
    type: 'income' | 'expense';
    type_display: string;
    sub_type: string;
    sub_type_display: string;
    amount: number;
    balance_after: number;
    landing_company: string;
    status: 'pending' | 'processing' | 'completed' | 'failed' | 'cancelled';
    status_display: string;
    summary: string;
    description?: string;
    transaction_time: string;
    created_at: string;
    updated_at: string;
    processed_at?: string;
    created_by?: number;
    processed_by?: number;
  }

  /** 子类型选项 */
  export interface SubTypeOption {
    value: string;
    label: string;
  }

  /** 分页响应结构 */
  export interface TransactionListResponse {
    data: TransactionRecord[];
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
    from: number | null;
    to: number | null;
    first_page_url: string | null;
    last_page_url: string | null;
    next_page_url: string | null;
    prev_page_url: string | null;
    path: string | null;
    links: Array<{
      url: string | null;
      label: string;
      active: boolean;
    }>;
  }

  /** API响应包装 */
  export interface ApiResponse<T = any> {
    success: boolean;
    data: T;
    message?: string;
    error?: any;
  }
}

/**
 * 获取交易账单列表
 * @param params 查询参数
 * @returns 交易账单列表响应数据
 */
export async function getTransactionListApi(params: TransactionApi.TransactionListParams) {
  return requestClient.get<TransactionApi.TransactionListResponse>('/api/tenant/transactions', {
    params,
  });
}

/**
 * 获取子类型选项
 * @param type 交易类型
 * @returns 子类型选项
 */
export async function getTransactionSubTypesApi(type?: 'income' | 'expense') {
  return requestClient.get<TransactionApi.SubTypeOption[]>('/api/tenant/transactions/sub-types', {
    params: type ? { type } : undefined,
  });
}

/**
 * 获取交易详情
 */
export async function getTransactionDetailApi(id: string) {
  return requestClient.get<TransactionApi.ApiResponse<TransactionApi.TransactionRecord>>(`/api/tenant/transactions/${id}`);
}

/**
 * 导出交易数据
 */
export async function exportTransactionDataApi(params: TransactionApi.TransactionListParams) {
  return requestClient.get<TransactionApi.ApiResponse<{ count: number; export_url: string }>>('/api/tenant/transactions/export', { params });
}